package windows;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import javax.swing.Timer;
import java.util.*;

import main.ImageLibrary;
import main.RoomLibrary;
import main.Telecommando;

import base.Button;
import base.Window;

public class Menu extends Window implements ActionListener {

	private Vector<Button> roomButtons = new Vector<Button>( );
	private Vector<Button> deviceButtons = new Vector<Button>( );
	private Button butFav = null;
//	private Button butSettings = null;
	private Hashtable<String,Window> devices = new Hashtable<String,Window>( );
	private String image = "Images/menu.png";
	private int x = 0;
	private int y = 0;
	private int initX = 0;
	private int initY = 0;
	private int addX = 0;
	private int addY = 0;
	private Timer timer;
	private int width = 0;
	private int height = 0;
	private int roomY = 0;
	private int deviceY = 0;
	public String selectedDevice = "";
	public String selectedRoom = "";
	private boolean scrollRooms = false;
	private boolean scrollDevices = false;
	private Rectangle clipRooms = new Rectangle( 0 , 0 , 31 , 218 );
	private Rectangle clipDevices = new Rectangle( 0 , 0 , 31 , 218 );
	private int scrollAddY = 0;
	private boolean horizontal = false;
	private boolean verticle = false;
	public Menu( )
	{
		super( "Menu" );
		ImageLibrary.add( image , image );
		//rooms.add( new Button( "Favs" , new Point( 0 , 0 ) , "room[0]" ) );//, true , true ) );
		//buttons.add( new Button( "Button 2" , new Point( 25 , 12 ) , "Images/bunny.png" , true ) );
		BufferedImage img = ImageLibrary.get( image );
		y = (clip.height >> 1) - (img.getHeight( ) >> 1);
		width = img.getWidth( );
		x = 42 - width;
		timer = new Timer( 5000 , this );
		timer.setRepeats( false );
		height = Telecommando.height;
		clip.height = height;
		clip.width = width;
		initX = x;
		initY = y;
		//Set<String> keys = RoomLibrary.rooms.keySet( );
		int yVal = 3;
		for( int key = 0; key < RoomLibrary.getNumberOfRooms( ); ++key )
		{
			for( int index = 0; index < RoomLibrary.getNumberOfRooms( key ); ++index )
			{
				//System.out.println( "ADDING BUTTON " + RoomLibrary.getName( key ) );
				if( RoomLibrary.getNumberOfRooms( key ) == 1 )
				{
					Button but = new Button( RoomLibrary.getName( key ) , new Point( initX + 5 , yVal ) , RoomLibrary.getImage( key ) );
					but.setColor( Color.black );
					roomButtons.add( but );
				}
				else
				{
					Button but = new Button( RoomLibrary.getName( key ) + "[" + index + "]" , new Point( initX + 5 , yVal ) , RoomLibrary.getImage( key ) );
					but.setColor( Color.black );
					roomButtons.add( but );
				}
				yVal += 43;
			}
		}
		addY = 23;
		butFav = new Button( "Favorites" , new Point( 6 , 13 ) , "fav[0]" , false , false );
		//roomHeight = yVal;
	}

	public void selectRoom( String name , int id )
	{
		deviceButtons.clear( );
		devices.clear( );
		int yVal = 0;
	//	Vector devs = RoomLibrary.get
		selectedRoom = name + "[" + id + "]";
		Collection<Window> windows = RoomLibrary.getDevices( name , id );
		if( windows == null )
			return;
		for( Window temp : windows )
		{
			Button but = new Button( temp.getName( ) , new Point( initX + 48 , yVal ) , "Images/bunny.png" );
			but.setColor( Color.black );
			deviceButtons.add( but );
			devices.put( but.getName( ) , temp );
		}
	}

	public void update( )
	{
		clip.width = width + x + addX;
		clip.height = height + y + addY;
		int index = 0;
		for( Button but : roomButtons )
		{
			but.setX( x + addX + 5 );
			if( scrollRooms )
				but.setY( y + addY + roomY + scrollAddY + 22 + index*43 );
			else
				but.setY( y + addY + roomY + 22 + index*43 );
			++index;
		}
		index = 0;
		for( Button but : deviceButtons )
		{
			but.setX( x + addX + 48 );
			if( scrollDevices )
				but.setY( y + addY + deviceY + scrollAddY + 22 + index*43 );
			else
				but.setY( y + addY + deviceY + 22 + index*43 );
			++index;
		}
	}

	public void resetLocation( )
	{
		x = initX;
		y = initY;
		addX = 0;
		//addY = 0;
		scrollAddY = 0;
		scrollRooms = false;
		scrollDevices = false;
		roomY = 0;
		deviceY = 0;
		horizontal = false;
		verticle = false;
		for( Button but : roomButtons )
		{
			but.setX( but.getInitRect( ).x );
			but.setY( but.getInitRect( ).y );
		}
		for( Button but : deviceButtons )
		{
			but.setX( but.getInitRect( ).x );
			but.setY( but.getInitRect( ).y );
		}
		timer.stop( );
	}
	@Override
	public void draw(Graphics2D g2d) {
		g2d.drawImage( ImageLibrary.get( image ) , x + addX , y + addY , Telecommando.getInstance( ) );
		drawRooms( g2d );
		drawDevices( g2d );
		butFav.draw( g2d );
	}

	private void drawRooms( Graphics2D g2d )
	{
		Shape clip = g2d.getClip( );
		int x = this.x + addX + 5;
		int y = this.y + addY + 22;
		clipRooms.setLocation( x , y );
		g2d.clip( clipRooms );
		for( Button but : roomButtons )
			but.draw( g2d );
		g2d.setClip( clip );
	}

	private void drawDevices( Graphics2D g2d )
	{
		Shape clip = g2d.getClip( );
		int x = this.x + addX + 48;
		int y = this.y + addY + 22;
		clipDevices.setLocation( x , y );
		g2d.clip( clipDevices );
		for( Button but : deviceButtons )
			but.draw( g2d );
		g2d.setClip( clip );
	}

	@Override
	public void fingerPressed(Point location) {
		timer.stop( );
		if( clipRooms.contains( location ) )
		{
			scrollRooms = true;
			for( Button but : roomButtons )
			{
				if( but.press( location.x , location.y ) )
					return;
			}
		}
		else if( clipDevices.contains( location ) )
		{
			scrollDevices = true;
			for( Button but : deviceButtons )
			{
				if( but.press( location.x , location.y ) )
					return;
			}
		}
	}

	@Override
	public void fingerReleased(Point location) {
		if( butFav.press( location.x , location.y ) )
		{
			String name = butFav.getImage( );
			int idBs = name.indexOf( '[' );
			if( idBs != -1 )
			{
				String strID = name.substring( idBs+1 , name.indexOf( "]" ) );
				int id = -1;
				try{ id = Integer.parseInt( strID ); } catch( Exception e ){ }
				if( id != -1 )
				{
					if( id == 1 )
					{
						//TODO REMOVE FROM FAVORITES
						butFav.setImage( "fav[0]" );
						RoomLibrary.removeFavorite( selectedDevice );
					}
					else
					{
						//TODO ADD TO FAVORITES
						butFav.setImage( "fav[1]" );
						RoomLibrary.addFavorite( selectedDevice );
					}
					if( !selectedRoom.equals( "" ) )
					{
						if( selectedRoom.startsWith( "Favorites" ) )
							selectRoom( "Favorites" , 0 );
					}
				}
			}
			butFav.release( );
		}
		for( Button but : roomButtons )
		{
			if( but.isPressed( ) )
			{
				but.release( );
				if( !verticle && !horizontal )
				{
					String name = but.getName( );
					int idBs = name.indexOf( '[' );
					if( idBs != -1 )
					{
						String room = name.substring( 0 , idBs );
						String strID = name.substring( idBs+1 , name.indexOf( "]" ) );
						int id = -1;
						try{ id = Integer.parseInt( strID ); } catch( Exception e ){ }
						if( id != -1 )
							selectRoom( room , id );
					}
					else
						selectRoom( name , 0 );
				}
			}
		}
		for( Button but : deviceButtons )
		{
			if( but.isPressed( ) )
			{
				but.release( );
				if( !verticle && !horizontal )
				{
					Telecommando.setWindow( devices.get( but.getName( ) ) );
					resetLocation( );
					if( RoomLibrary.isFavorite( but.getName( ) ) )
						butFav.setImage( "fav[1]" );
					else
						butFav.setImage( "fav[0]" );
					selectedDevice = but.getName( );
				}
			}
		}
		timer.restart( );
		x += addX;
		addX = 0;
		//y += addY;
		//addY = 0;
		if( x > 0 )
			x = 0;
		if( x < initX )
			x = initX;
		if( scrollRooms )
			roomY += scrollAddY;
		else if( scrollDevices )
			deviceY += scrollAddY;
		scrollAddY = 0;
		scrollRooms = false;
		scrollDevices = false;
		horizontal = false;
		verticle = false;
	}

	@Override
	public void gestureHorizontal(int amount) {
		if( !horizontal && !verticle )
			if( Math.abs( amount ) > 9 )
				horizontal = true;
		if( !horizontal )
			return;
		addX = amount;
		if( x + addX > 0 )
			addX = -x;
		if( x + addX < initX )
			addX = initX - x;
	}

	@Override
	public void gestureVertical(int amount) {
		if( !horizontal && !verticle )
			if( Math.abs( amount ) > 9 )
				verticle = true;
		if( !verticle )
			return;
		if( scrollRooms )
		{
			scrollAddY = amount;
			if( roomY + scrollAddY < -300 )
				scrollAddY = -300 - roomY;
			else if( roomY + scrollAddY > 0 )
				scrollAddY = -roomY;
		}
		if( scrollDevices )
		{
			scrollAddY = amount;
			if( deviceY + scrollAddY < -300 )
				scrollAddY = -300 - deviceY;
			else if( deviceY + scrollAddY > 0 )
				scrollAddY = -deviceY;
		}
	}
	//@Override
	public void actionPerformed(ActionEvent e) {
		if( e.getSource( ).equals( timer ) )
		{
			resetLocation( );
		}
	}
}
